﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using RBS.Core.Extension;
using RBS.Core.Model;
using RBS.Web.API.Models;
using JsonData = RBS.Common.JsonEntity.JsonData;

namespace RBS.Web.API.Areas.User.Controllers
{
    
    /// <summary>
    /// 订单详细
    /// </summary>
    public class OrderDetailController : ApiController
    {
        private ApiContext db = new ApiContext(); 
 
        /// <summary>
        /// 获取订单详细
        /// </summary>
        /// <param name="uid">食客主键ID</param>
        /// <param name="orderid">订单ID</param>
        /// <returns></returns>
        public JsonData GetOrderDetail(int uid, int orderid)
        {
            var jsonData = new JsonData{Success = true};
            var items = db.OrderDetails.Where(c => c.UId == uid && c.OrderId == orderid);
            jsonData.Data = items;
            return jsonData;
        } 
       /* /// <summary>
        /// 添加订单菜单详细
        /// </summary>
        /// <param name="model">Details实体</param>
        /// <returns></returns>
        public JsonData Post([FromBody]D_Order_Details model)
        {
            var jsonData = new JsonData();
            if (ModelState.IsValid)
            {
                var item = db.OrderDetails.FirstOrDefault(c => c.HCId == model.HCId && c.UId == model.UId && c.OrderId==model.OrderId);
                if (item == null)
                {
                    if (model.OrderId == -1)
                    {
                        var order = new RBS.Core.Model.D_Order
                            {
                                OrderState = -1,
                                OrderTime = DateTime.Now,
                                U_Id = model.UId
                            };
                        order = db.Order.Add(order);
                        db.SaveChanges();
                        model.OrderId = order.Id;
                    } 
                    db.OrderDetails.Add(model);
                    db.SaveChanges();
                }
                else
                {
                    item.Num = model.Num + item.Num;
                    db.Entry(item).State = EntityState.Modified;
                    db.SaveChanges();
                }
                jsonData.Success = true;
                var items = db.OrderDetails.Where(c => c.UId == model.UId && c.OrderId == model.OrderId);
                jsonData.Data = items;
                return jsonData;
            }
            else
            {
                jsonData.Success = false;
                jsonData.Error = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState).ToString();
                return jsonData;
            }
        }
       */


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public HttpResponseMessage Delete(int id, int type)
        {
            if (type == 1)
            {
                return DeleteDetailsById(id);
            }
            if (type == 2)
            {
                return DeleteDetailsByOrderId(id);
            }
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }


        /// <summary>
        /// 删除当前菜单菜谱
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private HttpResponseMessage DeleteDetailsById(int id)
        {
            D_Order_Details dOrderDetails = db.OrderDetails.Find(id);
            if (dOrderDetails == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            } 
            db.OrderDetails.Remove(dOrderDetails); 
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, dOrderDetails);
        }
        /// <summary>
        /// 删除临时菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns> 
        private HttpResponseMessage DeleteDetailsByOrderId(int id)
        { 
            foreach (var item in db.OrderDetails.Where(c => c.OrderId == id))
            {
                db.OrderDetails.Remove(item);
            } 
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            } 
            return Request.CreateResponse(HttpStatusCode.OK);
        }

    }

    
}
